import React from "react";
import { createBrowserRouter, Navigate, RouteObject } from "react-router-dom";

// 直接导入组件
import App from "../App";
import Login from "../pages/Login";
import Overview from "../pages/Overview";
import Users from "../pages/Users";
import Products from "../pages/Products";
import Orders from "../pages/Orders";

// 导入页面组件
import Authority from "../pages/School"; // School组件已更新为Authority页面
import Ads from "../pages/Shou"; // Shou组件已更新为Ads页面
import Operations from "../pages/Admin"; // Admin组件已更新为Operations页面
import { useAuthStore } from "../zus";
// Auth check component接口定义
interface AuthCheckProps {
  children: React.ReactNode;
}

// token验证
const AuthCheck: React.FC<AuthCheckProps> = ({ children }) => {
  return localStorage.getItem("token") ? (
    <>{children}</>
  ) : (
    <Navigate to="/login" />
  );
};
//路由权限筛选

let menu=useAuthStore.getState().menu
// console.log(menu,1111);

let allowpath=menu.map(i=>i.path)
const allpath=[
  { path: "/overview", element: <Overview /> },
  { path: "/authority", element: <Authority /> },
  { path: "/users", element: <Users /> },
  { path: "/products", element: <Products /> },
  { path: "/orders", element: <Orders /> },
  { path: "/ads", element: <Ads /> },
  { path: "/operations", element: <Operations /> },
]
const filterpath = allpath.filter(i => allowpath.includes(i.path)); // 恢复权限控制，只注册有权限的页面
// 路由配置
const routes: RouteObject[] = [
  {
    path: "/",
    element: (
      <AuthCheck>
        <App />
      </AuthCheck>
    ),
    children: [
      ...filterpath,
      { path: "/overview", element: <Overview /> },
      { path: "/authority", element: <Authority /> },
      { path: "/users", element: <Users /> },
      { path: "/products", element: <Products /> },
      { path: "/orders", element: <Orders /> },
      { path: "/ads", element: <Ads /> },
      { path: "/operations", element: <Operations /> },
      { path: "/", element: <Navigate to="/overview" /> },
      { path: "*", element: <Navigate to="/login" /> },
    ],
  },
  { path: "/login", element: <Login /> },
];

const router = createBrowserRouter(routes);

export default router;
